import os
from http.client import HTTPException

from home import db
from werkzeug.security import check_password_hash, generate_password_hash
from Hook.hook import getAvatarName
from home import redisDB
from home.config import BASE_DIR
from flask import Blueprint, request, redirect, make_response
from model import UserModel, EnterpriseModel, HRModel, JobModel, AdminModel, UserResumeModel, EInfoConfirmingModel, \
    EJobConfirmingModel, HRAndJobModel, HomeMenuModel, UserAndJobModel
import time

bp = Blueprint("admin", __name__, url_prefix="/admin")

@bp.route("/initData",methods=["GET"])
def initData():
    from sqlalchemy.exc import IntegrityError
    try:
        db.drop_all()

        db.create_all()
        HomeMenuModel.init_db()
        AdminModel.init_db()
        JobModel.init_db()
        UserModel.init_db()
        UserResumeModel.init_db()
        EnterpriseModel.init_db()
        HRModel.init_db()
        UserAndJobModel.init_db()
        HRAndJobModel.init_db()

    except IntegrityError as e:
        db.session.rollback()

        print(e.orig)
    return "初始化成功"

# 获取热门岗位-前10-做数据展示
@bp.route("/getTenHotJob", methods=["POST"])
def getTenHotJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    sql = "select jobId,COUNT(*) as count from user_job group by jobId LIMIT 10"
                    hotjobs = db.session.execute(sql).fetchall()
                    for i in range(len(hotjobs)):
                        job = JobModel.query.filter_by(jobId=hotjobs[i][0]).first()
                        msg["datas"].append(
                            {
                                "value": hotjobs[i][1],
                                "name": job.jobName,
                            }
                        )

                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"
    return msg

# 获取系统用户分布，目前只有求职者
@bp.route("/getUserAddress", methods=["POST"])
def getUserAddress():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    user = db.session.query(UserModel.newAddress).all()
                    li = list()
                    for i in user:
                        if i[0] is not None:
                            str = "".join(i).split("-")[0]
                            strList = ["黑龙江省", "内蒙古自治区"]
                            if str in strList:
                                str = str[0:3]
                            else:
                                str = str[0:2]
                            li.append(str)
                        else:
                            li.append("南海诸岛")
                    dic = {}
                    lis = []
                    for key in li:
                        dic[key] = dic.get(key, 0) + 1
                    for k, v in dic.items():
                        lis.append({"name": k, "value": v})
                    msg["datas"]=lis
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "success"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"
    return msg

# 管理员--将已审核岗位，进行处理
@bp.route("/adminAuditedJobs", methods=["POST"])
def adminAuditedJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]
    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    state = request.json["auditState"]
                    auditId = request.json["auditId"]
                    jobId=request.json["jobId"]
                    job = EJobConfirmingModel.query.filter_by(auditId=auditId).first()

                    if state == "已通过":
                        if job:
                            operationType = request.json["operationType"]
                            # 添加岗位，jobId未空，编辑岗位，存在jobId
                            # 添加岗位
                            if operationType == "添加岗位":
                                jobId = str(int(time.time() * 1000))
                                newJob = JobModel(
                                    jobId=jobId,
                                    jobName=job.jobName,
                                    jobAge=job.jobAge,
                                    jobEdu=job.jobEdu,
                                    jobEx=job.jobEx,
                                    jobNature=job.jobNature,
                                    jobAddress=job.jobAddress,
                                    jobSalary=job.jobSalary,
                                    jobIndustry=job.jobIndustry,
                                    isPublishing="true",
                                    jobState="正在招聘",
                                    jobDetail=job.jobDetail,
                                )
                                db.session.add(newJob)
                                db.session.commit()
                                hr_job = HRAndJobModel(
                                    jobId=jobId,
                                    hrId=request.json["hrId"]
                                )
                                db.session.add(hr_job)
                                db.session.commit()
                                job.auditState = state
                                job.auditTime = request.json["auditTime"]
                                job.isAudit = "true"
                                job.auditResultContent = request.json["auditResultContent"]
                                db.session.commit()
                                msg["message"] = "操作成功"
                                msg["code"] = "200"
                                msg["type"] = "success"
                            # 更新岗位
                            elif operationType == "编辑岗位":
                                updateJob = JobModel.query.filter_by(jobId=job.jobId).first()
                                if updateJob:
                                    updateJob.jobName = job.jobName
                                    updateJob.jobAddress = job.jobAddress
                                    updateJob.jobEdu = job.jobEdu
                                    updateJob.jobAge = job.jobAge
                                    updateJob.jobEx = job.jobEx
                                    updateJob.jobNature = job.jobNature
                                    updateJob.jobSalary = job.jobSalary
                                    updateJob.jobDetail = job.jobDetail
                                    updateJob.jobIndustry = job.jobIndustry
                                    db.session.commit()
                                    job.auditState = state
                                    job.auditTime = request.json["auditTime"]
                                    job.isAudit = "true"
                                    job.auditResultContent = request.json["auditResultContent"]

                                    db.session.commit()
                                    msg["message"] = "操作成功"
                                    msg["code"] = "200"
                                    msg["type"] = "success"
                                else:
                                    msg["message"] = "操作对象错误"
                                    msg["code"] = "500"
                                    msg["type"] = "error"
                        else:
                            msg["message"] = "审核表不存在该岗位"
                            msg["code"] = "500"
                            msg["type"] = "error"
                    elif state == "未通过":
                        job.auditState = state
                        job.auditTime = request.json["auditTime"]
                        job.isAudit = "true"
                        job.auditResultContent = request.json["auditResultContent"]
                        db.session.commit()
                        msg["message"] = "操作成功"
                        msg["code"] = "200"
                        msg["type"] = "success"

                    jobs=EJobConfirmingModel.query.filter_by(jobId=jobId).all()
                    if jobs:
                        from Hook.hook import compareDate
                        for j in jobs:
                            if compareDate(j.confirmTime,job.confirmTime) and j.auditId != job.auditId and j.isAudit !="true":
                                j.auditState = "未通过"
                                j.auditTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                                j.isAudit = "true"
                                j.auditResultContent = "审核过期"
                                db.session.commit()
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员--查看需要审核岗位信息  全部
@bp.route("/adminPreAuditJobs", methods=["POST"])
def adminPreAuditJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    global operationType
    operationType=""
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    auditId = request.json["auditId"]
                    job = EJobConfirmingModel.query.filter_by(auditId=auditId).first()
                    if job:

                        hr = HRModel.query.filter_by(hrId=job.hrId).first()
                        if job.operationType == "add":
                            operationType = "添加岗位"
                        elif job.operationType == "update":
                            operationType = "编辑岗位"
                        msg["datas"].append(
                            {
                                "auditId": job.auditId,
                                "jobId": job.jobId,
                                "jobName": job.jobName,
                                "jobAddress": job.jobAddress,
                                "jobNature": job.jobNature,
                                "jobEdu": job.jobEdu,
                                "jobAge": job.jobAge,
                                "jobEx": job.jobEx,
                                "jobSalary": job.jobSalary,
                                "jobDetail": job.jobDetail,
                                "jobIndustry": job.jobIndustry,

                                "hrId": job.hrId,
                                "hrName": job.hrName,
                                "hrEmail": job.hrEmail,
                                "hrPhone": job.hrPhone,

                                "isAudit": job.isAudit,
                                "auditTime": job.auditTime,
                                "confirmTime": job.confirmTime,
                                "auditState": job.auditState,
                                "operationType": operationType,
                                "eId": hr.e_hr_id,
                                "businessName": hr.e_hr.businessName,
                                "auditResultContent": job.auditResultContent,
                            }
                        )
                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# admin审核hr提交的岗位信息  部分
@bp.route("/adminGetAuditJobs", methods=["POST"])
def adminGetAuditJobs():
    global t
    t = ""
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "ename": [],
        "confirmtime": [],
        "audittime": [],
        "auditstate": [],
        "operationtype": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                job_lis = []
                ename_lis = []
                confirmtime_lis = []
                audittime_lis = []
                auditstate_lis = []
                operationType_lis = []
                if admin:

                    jobs = EJobConfirmingModel.query.all()
                    if jobs:
                        for job in jobs:
                            hr = HRModel.query.filter_by(hrId=job.hrId).first()
                            if job.operationType == "update":
                                t = "编辑岗位"

                            elif job.operationType == "add":
                                t = "添加岗位"
                            msg["datas"].append(
                                {
                                    "auditId": job.auditId,
                                    "jobId": job.jobId,
                                    "jobName": job.jobName,
                                    "jobIndustry": job.jobIndustry.split("-")[1],
                                    "hrId": job.hrId,
                                    # "hrName": job.hrName,
                                    "isAudit": job.isAudit,
                                    "auditTime": job.auditTime if job.auditTime else "null",
                                    "confirmTime": job.confirmTime,
                                    "auditState": job.auditState,
                                    "operationType": t,
                                    "eId": hr.e_hr_id,
                                    "businessName": hr.e_hr.businessName
                                }
                            )
                            job_lis.append(
                                {
                                    "text": job.jobIndustry.split("-")[1],
                                    "value": job.jobIndustry.split("-")[1],
                                }
                            )
                            ename_lis.append(
                                {
                                    "text": hr.e_hr.businessName,
                                    "value": hr.e_hr.businessName,
                                }
                            )

                            confirmtime_lis.append(
                                {
                                    "text": job.confirmTime.split(" ")[0],
                                    "value": job.confirmTime.split(" ")[0],
                                }
                            )

                            if job.auditTime is not None:
                                audittime_lis.append(
                                    {
                                        "text": job.auditTime.split(" ")[0],
                                        "value": job.auditTime.split(" ")[0],
                                    }
                                )
                            else:
                                audittime_lis.append(
                                    {
                                        "text": "null",
                                        "value": "null",
                                    }
                                )
                            auditstate_lis.append(
                                {
                                    "text": job.auditState,
                                    "value": job.auditState,
                                }
                            )
                            operationType_lis.append(
                                {
                                    "text": t,
                                    "value": t,
                                }
                            )
                        for job in job_lis:
                            if job not in msg["industrys"]:
                                msg["industrys"].append(job)
                        for e in ename_lis:
                            if e not in msg["ename"]:
                                msg['ename'].append(e)
                        for confirm in confirmtime_lis:
                            if confirm not in msg["confirmtime"]:
                                msg['confirmtime'].append(confirm)
                        for audit in audittime_lis:
                            if audit not in msg["audittime"]:
                                msg['audittime'].append(audit)

                        for state in auditstate_lis:
                            if state not in msg["auditstate"]:
                                msg['auditstate'].append(state)
                        for operation in operationType_lis:
                            if operation not in msg["operationtype"]:
                                msg['operationtype'].append(operation)

                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员--查看需要审核企业的具体信息
@bp.route("/adminPreAuditEnterprise", methods=["POST"])
def adminPreAuditEnterprise():
    global eAdvantage
    eAdvantage=""
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    auditId = request.json["auditId"]
                    e = EInfoConfirmingModel.query.filter_by(auditId=auditId).first()
                    if e:
                        if e.advantage:
                            eAdvantage="，".join(e.advantage.split("-"))
                            # msg["datas"].append(
                            #     {
                            #         "eAdvantage": "，".join(e.advantage.split("-")),
                            #     }
                            # )
                        else:
                            eAdvantage = ''

                        msg["datas"].append(
                            {
                                "auditId": e.auditId,
                                "eId": e.eId,
                                "eContent": e.content,
                                "eSize": e.size,
                                "eNature": e.nature,
                                "eIndustry": e.industry,
                                "eAdvantage":eAdvantage,

                                "contactName": e.contactName,
                                "contactPhone": e.contactPhone,
                                "contactEmail": e.contactEmail,
                                "contactAddress": e.contactAddress,

                                "businessName": e.businessName,
                                "businessSetTime": e.businessSetTime,
                                "businessRegCapital": e.businessRegCapital,
                                "businessLegalRepresentative": e.businessLegalRepresentative,

                                "hrId": e.hrId,
                                "hrName": e.hrName,
                                "hrEmail": e.hrEmail,
                                "hrPhone": e.hrPhone,

                                "isAudit": e.isAudit,
                                "auditTime": e.auditTime,
                                "confirmTime": e.confirmTime,
                                "auditState": e.auditState,
                                "operationType": e.operationType,
                                "auditResultContent": e.auditResultContent,
                            }
                        )
                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# admin已审核企业信息，将信息提交
@bp.route("/adminAuditedEnterprise", methods=["POST"])
def adminAuditedEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:

                    auditId = request.json["auditId"]
                    auditedE = EInfoConfirmingModel.query.filter_by(auditId=auditId).first()
                    eId=auditedE.eId
                    if auditId:
                        state = request.json["auditState"]

                        if state == "已通过":

                            e = EnterpriseModel.query.filter_by(eId=eId).first()
                            e.content = auditedE.content
                            e.size = auditedE.size
                            e.nature = auditedE.nature
                            e.industry = auditedE.industry
                            e.advantage = auditedE.advantage
                            e.contactName = auditedE.contactName
                            e.contactPhone = auditedE.contactPhone
                            e.contactEmail = auditedE.contactEmail
                            e.contactAddress = auditedE.contactAddress

                            auditedE.auditState = state
                            auditedE.auditTime = request.json["auditTime"]
                            auditedE.isAudit = "true"
                            auditedE.auditResultContent = request.json["auditResultContent"]

                            db.session.commit()
                            msg["message"] = "操作成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                        elif state == "未通过":
                            auditedE.auditState = state
                            auditedE.auditTime = request.json["auditTime"]
                            auditedE.isAudit = "true"
                            auditedE.auditResultContent = request.json["auditResultContent"]
                            db.session.commit()
                            msg["message"] = "操作成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                    else:
                        msg["message"] = "非法操作2"
                        msg["code"] = "500"
                        msg["type"] = "error"
                    es = EInfoConfirmingModel.query.filter_by(eId=eId).all()
                    if es:
                        from Hook.hook import compareDate
                        for e1 in es:
                            if compareDate(e1.confirmTime,auditedE.confirmTime) and e1.auditId != auditedE.auditId and e1.isAudit != "true":
                                e1.auditState = "未通过"
                                e1.auditTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                                e1.isAudit = "true"
                                e1.auditResultContent = "审核过期"
                                db.session.commit()
                # except Exception:
                #     print(Exception)
                #     msg["message"] = "非法操作1"
                #     msg["code"] = "500"
                #     msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"


    return msg


# 获取admin审核hr提交的企业信息
@bp.route("/adminGetAuditEnterprise", methods=["POST"])
def adminGetAuditEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "ename":[],
        "hrname": [],
        "confirmtime": [],
        "audittime": [],
        "auditstate": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    ename_lis = []
                    hrname_lis = []
                    confirmtime_lis = []
                    audittime_lis = []
                    auditstate_lis = []
                    es = EInfoConfirmingModel.query.all()
                    if es:
                        for e in es:
                            msg["datas"].append(
                                {
                                    "auditId": e.auditId,
                                    "eId": e.eId,
                                    "businessName": e.businessName,
                                    "hrId": e.hrId,
                                    "hrName": e.hrName,
                                    "isAudit": e.isAudit,
                                    "auditTime": e.auditTime if e.auditTime else 'null',
                                    "confirmTime": e.confirmTime ,
                                    "auditState": e.auditState,
                                }
                            )
                            ename_lis.append(
                                {
                                    "text": e.businessName,
                                    "value": e.businessName,
                                }
                            )
                            hrname_lis.append(
                                {
                                    "text": e.hrName,
                                    "value": e.hrName,
                                }
                            )
                            confirmtime_lis.append(
                                {
                                    "text": e.confirmTime.split(" ")[0],
                                    "value": e.confirmTime.split(" ")[0],
                                }
                            )

                            if e.auditTime is not None:
                                audittime_lis.append(
                                    {
                                        "text": e.auditTime.split(" ")[0],
                                        "value": e.auditTime.split(" ")[0],
                                    }
                                )
                            else:
                                audittime_lis.append(
                                    {
                                        "text": "null",
                                        "value": "null",
                                    }
                                )
                            auditstate_lis.append(
                                {
                                    "text": e.auditState,
                                    "value": e.auditState,
                                }
                            )
                        for e in ename_lis:
                            if e not in msg["ename"]:
                                msg['ename'].append(e)
                        for hr in hrname_lis:
                            if hr not in msg["hrname"]:
                                msg['hrname'].append(hr)
                        for confirm in confirmtime_lis:
                            if confirm not in msg["confirmtime"]:
                                msg['confirmtime'].append(confirm)
                        for audit in audittime_lis:
                            if audit not in msg["audittime"]:
                                msg['audittime'].append(audit)
                        for state in auditstate_lis:
                            if state not in msg["auditstate"]:
                                msg['auditstate'].append(state)

                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "没有数据"
                        msg["code"] = "500"
                        msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员撤销岗位
@bp.route("/adminRevokeJob", methods=["POST"])
def adminRevokeJob():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    try:
                        job = JobModel.query.filter_by(jobId=request.json["jobId"]).first()
                        if job:
                            job.isPublishing = "false"
                            job.jobState = "已被撤销"
                            db.session.commit()
                            msg["message"] = "操作成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                        else:
                            msg["message"] = "无该岗位信息"
                            msg["code"] = "500"
                            msg["type"] = "error"
                    except Exception:
                        print(Exception)
                        msg["message"] = "获取失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员初始化密码
@bp.route("/adminInitPwd", methods=["POST"])
def adminInitPwd():
    msg = {
        "message": "",
        "type": "",
        "code": "",
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    try:
                        initPart = request.json["initPart"]
                        if initPart == "hr":
                            hr = HRModel.query.filter_by(hrId=request.json["hrId"]).first()
                            if hr:
                                hr.hrPassword = generate_password_hash("123456")
                                msg["message"] = "初始化密码成功"
                                msg["code"] = "200"
                                msg["type"] = "success"
                            else:
                                msg["message"] = "HR不存在"
                                msg["code"] = "500"
                                msg["type"] = "error"
                        elif initPart == "user":
                            user = UserModel.query.filter_by(uId=request.json["uId"]).first()
                            if user:
                                user.password = generate_password_hash("123456")
                                msg["message"] = "初始化密码成功"
                                msg["code"] = "200"
                                msg["type"] = "success"
                            else:
                                msg["message"] = "User不存在"
                                msg["code"] = "500"
                                msg["type"] = "error"
                        else:
                            msg["message"] = "非法操作"
                            msg["code"] = "500"
                            msg["type"] = "error"
                    except Exception:
                        print(Exception)
                        msg["message"] = "获取失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员获取所有Job
@bp.route("/adminGetAllJobs", methods=["POST"])
def adminGetAllJobs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "industrys": [],
        "es": [],
        "jobStates": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                job_lis = []
                e_lis = []
                state = []
                if admin:
                    try:
                        jobs = JobModel.query.all()
                        if jobs:
                            for job in jobs:
                                msg["datas"].append(
                                    {
                                        "jobId": job.jobId,
                                        "jobName": job.jobName,
                                        "jobAddress": job.jobAddress,
                                        "jobEdu": job.jobEdu,
                                        "jobAge": job.jobAge,
                                        "jobEx": job.jobEx,
                                        "jobSalary": job.jobSalary,
                                        "isPublishing": job.isPublishing,
                                        "jobState": job.jobState,
                                        "jobIndustry": job.jobIndustry.split("-")[1],
                                        "eId": job.hr_list[0].e_hr.eId,
                                        "eName": job.hr_list[0].e_hr.businessName,
                                        "contactPhone": job.hr_list[0].e_hr.contactPhone,
                                    }
                                )
                                job_lis.append(
                                    {
                                        "text": job.jobIndustry.split("-")[1],
                                        "value": job.jobIndustry.split("-")[1],
                                    }
                                )
                                e_lis.append(
                                    {
                                        "text": job.hr_list[0].e_hr.businessName,
                                        "value": job.hr_list[0].e_hr.businessName,
                                    }
                                )
                                state.append(
                                    {
                                        "text": job.jobState,
                                        "value": job.jobState,
                                    }
                                )
                            for i in job_lis:
                                if i not in msg["industrys"]:
                                    msg["industrys"].append(i)
                            for i in e_lis:
                                if i not in msg["es"]:
                                    msg["es"].append(i)
                            for i in state:
                                if i not in msg["jobStates"]:
                                    msg["jobStates"].append(i)
                            msg["message"] = "获取成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                        else:
                            msg["message"] = "没有岗位发布"
                            msg["code"] = "500"
                            msg["type"] = "error"
                    except Exception:
                        print(Exception)
                        msg["message"] = "获取失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员获取所有User
@bp.route("/adminGetAllUsers", methods=["POST"])
def adminGetAllUsers():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "gander":[],
        "address":[],
        "education":[]
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                gander_lis=[]
                address_lis=[]
                education_lis=[]
                if admin:
                    # try:
                        users = UserModel.query.all()
                        if users:
                            for user in users:
                                userresume = UserResumeModel.query.filter_by(uId=user.uId).first()
                                if userresume:
                                    msg["datas"].append(
                                        {
                                            "uId": user.uId,
                                            "name": user.name,
                                            "gander": user.gander if user.gander else "",
                                            "phone": user.phone if user.phone else "",
                                            "email": user.email,
                                            "address": user.newAddress if user.newAddress else "",
                                            "education": userresume.eduEducation if userresume.eduEducation else "",
                                            "schoolName": userresume.eduSchoolName if userresume.eduSchoolName else "",
                                        }
                                    )
                                    gander_lis.append(
                                        {
                                            "text": user.gander,
                                            "value": user.gander,
                                        }
                                    )
                                    address_lis.append(
                                        {
                                            "text": user.newAddress.split("-")[0],
                                            "value": user.newAddress.split("-")[0],
                                        }
                                    )
                                    education_lis.append(
                                        {
                                            "text": userresume.eduEducation,
                                            "value": userresume.eduEducation,
                                        }
                                    )
                                else:
                                    msg["datas"].append(
                                        {
                                            "uId": user.uId,
                                            "name": user.name,
                                            "gander": user.gander,
                                            "phone": user.phone,
                                            "email": user.email,
                                            "address": user.newAddress,
                                            "education": "null",
                                            "schoolName": "null",
                                        }
                                    )
                                    gander_lis.append(
                                        {
                                            "text": user.gander,
                                            "value": user.gander,
                                        }
                                    )
                                    address_lis.append(
                                        {
                                            "text": user.newAddress,
                                            "value": user.newAddress,
                                        }
                                    )
                                    education_lis.append(
                                        {
                                            "text": "null",
                                            "value": "null",
                                        }
                                    )
                            for g in gander_lis:
                                    if g not in msg["gander"]:
                                        msg["gander"].append(g)
                            for a in address_lis:
                                    if a not in msg["address"]:
                                        msg["address"].append(a)
                            for e in education_lis:
                                    if e not in msg["education"]:
                                        msg["education"].append(e)
                            msg["message"] = "获取成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                        else:
                            msg["message"] = "没有用户"
                            msg["code"] = "500"
                            msg["type"] = "error"

                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员删除hr
@bp.route("/adminDelectHR", methods=["POST"])
def adminDelectHR():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    try:
                        hrId = request.json["hrId"]
                        hr = HRModel.query.filter_by(hrId=hrId).first()
                        if list(hr.hr_jobs):
                            msg["message"] = "该HR不可删除"
                            msg["code"] = "501"
                            msg["type"] = "error"
                        else:
                            db.session.delete(hr)
                            db.session.commit()
                            msg["message"] = "删除成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                    except Exception:
                        print(Exception.args)
                        msg["message"] = "删除失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员删除企业
@bp.route("/adminDelectEnterprise", methods=["POST"])
def adminDelectEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    try:
                        eId = request.json["eId"]
                        e = EnterpriseModel.query.filter_by(eId=eId).first()
                        if list(e.hrs):
                            msg["message"] = "该企业不可删除"
                            msg["code"] = "501"
                            msg["type"] = "error"
                        else:
                            db.session.delete(e)
                            db.session.commit()
                            msg["message"] = "删除成功"
                            msg["code"] = "200"
                            msg["type"] = "success"

                    except Exception:
                        print(Exception)
                        msg["message"] = "删除失败"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员添加公司
@bp.route("/adminAddEnterprise", methods=["POST"])
def adminAddEnterprise():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    # try:
                        eName=request.json["businessName"]
                        e1=EnterpriseModel.query.filter_by(businessName=eName).first()
                        if e1:
                            msg["message"] = "该企业已入驻，不可添加"
                            msg["code"] = "301"
                            msg["type"] = "error"
                        else:
                            e = EnterpriseModel(
                                eId=str(int(time.time() * 1000)),
                                avatarName="iwu.jpeg",
                                size=request.json["eSizeMin"] + "-" + request.json["eSizeMax"],
                                nature=request.json["eNature"],
                                industry=request.json["eIndustry"],
                                businessName=eName,
                                businessSetTime=request.json["businessSetTime"],
                                businessRegCapital=request.json["businessRegCapital"],
                                businessLegalRepresentative=request.json["businessLegalRepresentative"],
                            )
                            db.session.add(e)
                            db.session.commit()
                            msg["message"] = "入驻成功"
                            msg["code"] = "200"
                            msg["type"] = "success"

                    # except Exception:
                    #
                    #     msg["message"] = "添加失败"
                    #     msg["code"] = "500"
                    #     msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员获取所有HR
@bp.route("/adminGetALLHRs", methods=["POST"])
def adminGetALLHRs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
        "es": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                e_lis = []
                if admin:
                    hrs = HRModel.query.all()
                    if hrs:
                        for hr in hrs:
                            msg["datas"].append(
                                {
                                    "eId": hr.e_hr.eId,
                                    "eName": hr.e_hr.businessName,
                                    "hrId": hr.hrId,
                                    "hrName": hr.hrName,
                                    "hrPhone": hr.hrPhone,
                                    "hrLevel": hr.hrLevel,
                                    "hrEmail": hr.hrEmail,
                                }
                            )

                            e_lis.append(
                                {
                                    "text":hr.e_hr.businessName,
                                    "value": hr.e_hr.businessName,
                                }
                            )
                        for e in e_lis:
                            if e not in msg["es"]:
                                msg["es"].append(e)
                        msg["message"] = "获取成功"
                        msg["code"] = "200"
                        msg["type"] = "success"

                    else:
                        msg["message"] = "没有HR数据"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 管理员获取所有公司
@bp.route("/adminGetALLEs", methods=["POST"])
def adminGetALLEs():
    msg = {
        "message": "",
        "type": "",
        "code": "",
        "datas": [],
    }
    # 检查身份
    loginId = request.json["loginId"]
    # 获取用户的身份
    identity = request.json["identity"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    es = EnterpriseModel.query.all()
                    if es:
                        for e in es:
                            msg["datas"].append(
                                {
                                    "eId": e.eId,
                                    "businessName": e.businessName,
                                    "businessSetTime": e.businessSetTime,
                                    "eNature": e.nature,
                                    "eIndustry": e.industry,
                                }
                            )
                            msg["message"] = "获取成功"
                            msg["code"] = "200"
                            msg["type"] = "success"
                    else:
                        msg["message"] = "没有企业数据"
                        msg["code"] = "500"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 修改admin头像
@bp.route("/upAdminAvatar", methods=["POST"])
def upAdminAvatar():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    loginId = request.form["loginId"]
    # 获取用户的身份
    identity = request.form["identity"]
    token = request.form["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    # # 请求中获取到上传的图片
                    # avatar_data = request.files.get('file')
                    # # 获取头像具体格式，jpg、jpeg、、、、
                    # avatar_type = avatar_data.content_type.split("/")
                    # # 随机头像在本地的名称
                    # avatar_name = hook.getRandom() + "." + avatar_type[1]
                    # # 保存头像的路径
                    # path = BASE_DIR / "static/img/"
                    # # 头像的绝对路径
                    # avatar_path = os.path.join(path, avatar_name)
                    # avatar_data.save(avatar_path)  # 保存
                    avatar_name = getAvatarName(request.files.get('file'))
                    if avatar_name:
                        admin.adminAvatar = avatar_name
                        db.session.commit()
                        msg["message"] = "上传成功"
                        msg["type"] = "success"
                        msg["code"] = "200"
                    else:
                        msg["message"] = "上传失败"
                        msg["type"] = "error"
                        msg["code"] = "500"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 修改admin信息
@bp.route("/upAdminInfo", methods=["POST"])
def upAdminInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    if admin.adminEmail != request.json["adminEmail"]:
                        msg["message"] = "您邮箱已被修改，请重新登录"
                        msg["code"] = "201"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "修改成功"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    admin.adminName = request.json["adminName"]
                    admin.adminEmail = request.json["adminEmail"]
                    admin.adminPhone = request.json["adminPhone"]
                    db.session.commit()
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 修改admin密码
@bp.route("/upAdminPwd", methods=["POST"])
def upAdminPwd():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    oldPwd = request.json["oldPwd"]
                    if check_password_hash(admin.adminPassword, oldPwd):
                        newPwd = request.json["newPwd"]
                        admin.adminDetail = request.json["adminDetail"]
                        admin.adminPassword = generate_password_hash(newPwd)
                        db.session.commit()
                        msg["message"] = "密码修改成功，请重新登录"
                        msg["code"] = "200"
                        msg["type"] = "success"
                    else:
                        msg["message"] = "密码错误，请重新填写"
                        msg["code"] = "501"
                        msg["type"] = "error"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取admin信息
@bp.route("/getAdminInfo", methods=["POST"])
def getAdminInfo():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]
    token = request.json["token"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    msg["adminId"] = admin.adminId
                    msg["adminName"] = admin.adminName
                    msg["adminEmail"] = admin.adminEmail
                    msg["adminPhone"] = admin.adminPhone
                    msg["adminDetail"] = admin.adminDetail
                    msg["message"] = "获取成功"
                    msg["code"] = "200"
                    msg["type"] = "successs"
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"

            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"

        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"

    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"

    return msg


# 获取admin头像
@bp.route("/getAdminAvatar", methods=["POST"])
def getAdminAvatar():
    msg = {
        "message": "",
        "type": "",
        "code": "",

    }
    # 检查身份
    identity = request.json["identity"]
    loginId = request.json["loginId"]

    if loginId:
        res = redisDB.handle_redis_token(loginId)
        if res:
            if identity == "admin":
                admin = AdminModel.query.filter_by(adminId=loginId).first()
                if admin:
                    # 拼接图片地址
                    image_url = BASE_DIR / "static/img" / admin.adminAvatar
                    # image_url = BASE_DIR / "static/avatar_1.jpeg"
                    # print(user.avatarName)
                    with open(image_url, "rb") as f:
                        image_data = f.read()
                    image_res = make_response(image_data)
                    image_res.headers['Content-Type'] = 'image.jpeg/png'
                    return image_res
                else:
                    msg["message"] = "admin不存在"
                    msg["code"] = "500"
                    msg["type"] = "error"
                    return msg
            else:
                msg["message"] = "身份非法"
                msg["code"] = "500"
                msg["type"] = "error"
                return msg
        else:
            msg["message"] = "用户身份过期，请重新登录"
            msg["code"] = "300"
            msg["type"] = "error"
            return msg
    else:
        msg["message"] = "用户ID非法"
        msg["code"] = "500"
        msg["type"] = "error"
        return msg
